# coding=utf-8
from ToolsConfig import *
import copy

def FixList(_list, _len, defaultValue):
    """
    修正列表到目标长度
    :param _list:列表
    :param _len:目标长度
    :param defaultValue:默认值
    :return:
    """
    _list = _list if _list else [copy.deepcopy(defaultValue) for _ in range(_len)]
    _list = _list[:_len]
    if _len > len(_list): _list += [copy.deepcopy(defaultValue) for _ in range(len(_list), _len)]
    return _list


def GetPlayerHasAut(playerId):
    """
    判断玩家是否拥有创造和管理员权限
    :param playerId:
    :return:
    """
    return serverCompFactory.CreatePlayer(playerId).GetPlayerOperation() == 2 and \
        serverCompFactory.CreateGame(LevelId).GetPlayerGameType(playerId) == 1


def NbtTranslator(nbtDict):
    """
    将nbt格式的数据转换为python数据
    :param nbtDict:
    :return:
    """
    if nbtDict is None:
        return {}
    if type(nbtDict) is list:
        return NbtList(nbtDict)
    elif type(nbtDict) is dict and nbtDict.get('__type__', None) is None:
        return NbtDict(nbtDict)
    else:
        return NbtValue(nbtDict)


def NbtValue(nbtDict):
    """
    处理数Nbt数据
    :param nbtDict:
    :return:
    """
    if type(nbtDict) is dict:
        valueType = nbtDict.get('__type__', 0)
        return NbtTranslatorFunc[valueType - 1](nbtDict['__value__']) if valueType else 0
    else:
        return nbtDict


def NbtDict(nbtDict):
    """
    处理字典型Nbt数据
    :param nbtDict:
    :return:
    """
    value = {}
    for _ in nbtDict.items():
        value[_[0]] = NbtTranslator(_[1])
    return value


def NbtList(nbtList):
    """
    处理列表型Nbt数据
    :param nbtList:
    :return:
    """
    value = []
    for _ in nbtList:
        value.append(NbtTranslator(_))
    return value


def TransBool(nbtValue):
    """
    处理bool数据
    """
    if nbtValue == 2:
        return None
    return bool(nbtValue)


NbtTranslatorFunc = [
    TransBool, int, int, int, float, int, bytearray, str, list, NbtDict, list
]


def UpdateFunction(old_func, new_func):
    """
    热更新函数
    """
    old_func.__doc__ = new_func.__doc__
    old_func.__dict__ = new_func.__dict__
    old_func.__defaults__ = new_func.__defaults__
    old_func.__code__ = new_func.__code__


mounts = {'s': [], 'c': []}


def Mount(system):
    # type:(Union[clientBaseSystem,serverBaseSystem]) -> Any
    """
    挂载系统,为非系统类添加属性blueFoxSystem
    服务端只挂载BlueFoxServerSystem,客户端只挂载BlueFoxClientSystem
    """

    def _mount(cls):
        if issubclass(system, serverBaseSystem):
            mounts['s'].append(cls)
        elif issubclass(system, clientBaseSystem):
            mounts['c'].append(cls)
        return cls

    return _mount


def GetLastSubclass(cls, basicClass=None):
    """
    获取类的最终继承类
    :param cls:
    :param basicClass:
    :return:
    """
    if basicClass is None:
        basicClass = []
    subclasses = cls.__subclasses__()
    for subclass in subclasses:
        if subclass.__subclasses__():
            GetLastSubclass(subclass, basicClass)
        else:
            basicClass.append(subclass)
    return basicClass


def AddTupleContext(list1, list2):
    # type:(Union[Tuple,List],Union[Tuple,List]) -> Union[Tuple[Any,...],Tuple[int,int,int,int]]
    """
    将两个相同长度的tuple or list内容彼此相加,并返回新的tuple
    :return:list1 + list2
    """
    if list2.__len__() == list1.__len__():
        newList = [list1[n] + list2[n] for n in range(list1.__len__())]
        return tuple(newList)
    return tuple([])


def AddTupleContextReList(list1, list2):
    """
    将两个相同长度的tuple or list内容彼此相加,并返回新的list
    :return:list1 + list2
    """
    if list2.__len__() == list1.__len__():
        newList = [list1[n] + list2[n] for n in range(list1.__len__())]
        return newList
    return []


def GetDistance(pos1, pos2):
    # type:(Union[List,Tuple],Union[List, Tuple]) -> int
    """
    计算三维上两点距离
    :return:
    """
    x1, y1, z1 = pos1
    x2, y2, z2 = pos2
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2) ** 0.5


def ReduceTupleContext(list1, list2):
    # type:(Union[Tuple,List],Union[Tuple,List]) -> Union[Tuple[Any,...],Tuple[int,int,int,int]]
    """
    将两个相同长度的tuple or list内容彼此相减,并返回新的tuple
    :return:list1 - list2
    """
    if list2.__len__() == list1.__len__():
        newList = [list1[n] - list2[n] for n in range(list1.__len__())]
        return tuple(newList)
    return tuple([])
